
Namespace CommonDialog

    Public Class OpenFileDialog

#Region " Private Declarations "

        Private _bolCheckFileExists As Boolean = False
        Private _bolCheckPathExists As Boolean = True
        Private _bolMultiSelect As Boolean = False
        Private _bolValidateNames As Boolean = True
        Private _strFileName As String = String.Empty
        Private _strFileNames As String() = {""}
        Private _strFilter As String = String.Empty
        Private _strInitialDirectory As String = String.Empty
        Private _strTitle As String = "Select File To Open"

#End Region

#Region " Properties "

        ''' <summary>
        ''' Gets or sets a value indicating whether a file dialog displays a warning if the user specifies a filename that does not exist. The default is false.
        ''' </summary>
        Public Property CheckFileExists() As Boolean
            Get
                Return _bolCheckFileExists
            End Get
            Set(ByVal Value As Boolean)
                _bolCheckFileExists = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value that specifies whether warnings are displayed if the user types invalid paths and filenames. The default is true.
        ''' </summary>
        Public Property CheckPathExists() As Boolean
            Get
                Return _bolCheckPathExists
            End Get
            Set(ByVal Value As Boolean)
                _bolCheckPathExists = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets a string that is the full path of the file selected in the file dialog. The default is Empty. 
        ''' </summary>
        Public ReadOnly Property FileName() As String
            Get
                Return _strFileName
            End Get
        End Property

        ''' <summary>
        ''' Gets an array of string that contains one filename for each selected file. The default is an array with a single item whose value is Empty. 
        ''' </summary>
        Public ReadOnly Property FileNames() As String()
            Get
                Return _strFileNames
            End Get
        End Property

        ''' <summary>
        ''' Gets or sets the current filename filter string, which determines the choices that appear in the "Files of type" box at the bottom of a file dialog.  Filter string consists of one or more pairs of string values, all separated by the pipe symbol ( | ).  Example : "Saved Report PDF Files(*.pdf)|*.pdf|All Files (*.*)|*.*"
        ''' </summary>
        Public Property Filter() As String
            Get
                Return _strFilter
            End Get
            Set(ByVal Value As String)
                _strFilter = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the initial directory displayed by the file dialog box. 
        ''' </summary>
        Public Property InitialDirectory() As String
            Get
                Return _strInitialDirectory
            End Get
            Set(ByVal Value As String)
                _strInitialDirectory = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets an option indicating whether OpenFileDialog allows users to select multiple files. The default value is false.
        ''' </summary>
        Public Property MultiSelect() As Boolean
            Get
                Return _bolMultiSelect
            End Get
            Set(ByVal Value As Boolean)
                _bolMultiSelect = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the text that appears in the title bar of a file dialog. The default value is "Select File To Open"
        ''' </summary>
        Public Property Title() As String
            Get
                Return _strTitle
            End Get
            Set(ByVal Value As String)
                _strTitle = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether the dialog accepts only valid Win32 filenames. The default values is true.
        ''' </summary>
        Public Property ValidateNames() As Boolean
            Get
                Return _bolValidateNames
            End Get
            Set(ByVal Value As Boolean)
                _bolValidateNames = Value
            End Set
        End Property

#End Region

#Region " Constructors "

        Public Sub New()
        End Sub

#End Region

#Region " Methods "

        ''' <summary>
        ''' Displays the open file dialog for the current operating system.  Returns true if the user selected one ore more file names.  Read the FileName or FileNames property for the selected file(s).
        ''' </summary>
        Public Function ShowDialog() As Boolean

            Dim objDialogOpenFile As New VistaDialogs.VistaOpenFileDialog
            Using objDialogOpenFile
                objDialogOpenFile.CheckFileExists = Me.CheckFileExists
                objDialogOpenFile.CheckPathExists = Me.CheckPathExists
                objDialogOpenFile.Filter = Me.Filter
                objDialogOpenFile.InitialDirectory = Me.InitialDirectory
                objDialogOpenFile.Multiselect = Me.MultiSelect
                objDialogOpenFile.Title = Me.Title
                objDialogOpenFile.ValidateNames = Me.ValidateNames

                Dim objResult As System.Windows.Forms.DialogResult = objDialogOpenFile.ShowDialog

                If objResult = Forms.DialogResult.OK Then
                    _strFileName = objDialogOpenFile.FileName

                    'user selected one or more files
                    If Me.MultiSelect = True Then
                        _strFileNames = objDialogOpenFile.FileNames
                    End If

                    objDialogOpenFile.Dispose()
                    Return True

                Else
                    'user cancelled
                    objDialogOpenFile.Dispose()
                    Return False
                End If

            End Using
        End Function

#End Region

    End Class

End Namespace
